package mx.com.mindbits.bandpay.utils;


import mx.com.mindbits.bandpay.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CardDialogHost extends Dialog {
	private EditText host;
	private EditText port;
	private Button acept;
	private String _host;
	private String _port;

	public CardDialogHost(Context context, String _h, String _p) {
		super(context);
		
		this._host = _h.isEmpty() ? "" :_h;
		this._port = _p.isEmpty() ? "" :_p;
	}
	public interface DialogListener{
		void finish(String host, String port);
	}
	
	DialogListener dialogListener;
		
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_dialog);
		host = (EditText) findViewById(R.id.et_smpt);
		port = (EditText) findViewById(R.id.et_port);
		acept = (Button) findViewById(R.id.bt_host);
		
		if(!(_host.isEmpty()) && !(_port.isEmpty())){
			host.setText(_host);
			port.setText(_port);
			acept.setEnabled(true);
			acept.setBackgroundResource(R.drawable.blue_button_shape);
		}else{
			acept.setEnabled(false);
			acept.setBackgroundResource(R.drawable.button_disable_shape);
		}
		
		host.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable s) {}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {}
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if(s.length() > 0 && Utils.validaSMTP(host.getText().toString())){
					acept.setEnabled(true);
					acept.setBackgroundResource(R.drawable.blue_button_shape);
				}else{
					acept.setEnabled(false);
					acept.setBackgroundResource(R.drawable.button_disable_shape);
				}
				
			}
			
		});
		
		acept.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				dialogListener.finish(host.getText().toString(),
						port.getText().toString());
				CardDialogHost.this.dismiss();
			}
			
		});
	}
	
	
	public void setDialogResult(DialogListener listener){
		dialogListener = listener;
	}
	
	
	
}
